package cn.funeralobjects.dictms.converter;

import cn.funeralobjects.util.CollectionUtils;
import cn.funeralobjects.util.StringUtils;

import javax.persistence.AttributeConverter;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author FuneralObjects
 * Create date: 2020/6/3 4:31 AM
 */
public class DictRelateRuleConverter implements AttributeConverter<List<Integer>, String> {
    private static final String DELIMITER = ":";

    @Override
    public String convertToDatabaseColumn(List<Integer> attribute) {
        return CollectionUtils.isEmpty(attribute) ? "" : CollectionUtils.join(attribute, DELIMITER);
    }

    @Override
    public List<Integer> convertToEntityAttribute(String dbData) {
        return StringUtils.hasLength(dbData) ? CollectionUtils.split(dbData, DELIMITER).stream().map(Integer::valueOf).collect(Collectors.toList()) : Collections.emptyList();
    }
}
